/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.bus.service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.bus.entity.Vaccine;
import com.thinkgem.jeesite.modules.bus.entity.VaccineEnt;
import com.thinkgem.jeesite.modules.sys.utils.DictUtils;
import org.apache.commons.collections.ListUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.CrudService;
import com.thinkgem.jeesite.modules.bus.entity.SubsidyStandard;
import com.thinkgem.jeesite.modules.bus.dao.SubsidyStandardDao;

/**
 * 补助标准Service
 * @author sy
 * @version 2019-06-11
 */
@Service
@Transactional(readOnly = true)
public class SubsidyStandardService extends CrudService<SubsidyStandardDao, SubsidyStandard> {

	@Autowired
	private VaccineEntService vaccineEntService;
	@Qualifier("subsidyStandardDao")
	@Autowired
	private SubsidyStandardDao subsidyStandardDao;

	public SubsidyStandard get(String id) {
		return super.get(id);
	}
	
	public List<SubsidyStandard> findList(SubsidyStandard subsidyStandard) {
		return super.findList(subsidyStandard);
	}
	
	public Page<SubsidyStandard> findPage(Page<SubsidyStandard> page, SubsidyStandard subsidyStandard) {
		return super.findPage(page, subsidyStandard);
	}

	/**
	 *
	 * @Title: save
	 * @author: sy
	 * @date: 2019年06月11日 15:22
	 * @Description: 保存
	 * @param subsidyStandard
	 * @return: void
	 */
	@Transactional(readOnly = false)
	public void save(SubsidyStandard subsidyStandard) {
		subsidyStandard.setAnimalTypeName(DictUtils.getDictLabel(subsidyStandard.getAnimalTypeCode(),"animalType",""));
		subsidyStandard.setStandardTypeName(DictUtils.getDictLabel(subsidyStandard.getStandardTypeCode(),"standardType",""));
		List<VaccineEnt> list = subsidyStandard.getEntList();
		List<String> idList = new ArrayList<>();
		List<String> nameList = new ArrayList<>();
		if(null != list){
			for (VaccineEnt vaccineEnt :list){
				idList.add(vaccineEnt.getId());
				nameList.add(vaccineEntService.get(vaccineEnt.getId()).getName());
			}
			subsidyStandard.setVaccineEntId(StringUtils.join(idList, ","));
			subsidyStandard.setVaccineEntName(StringUtils.join(nameList, ","));
		}
		super.save(subsidyStandard);
	}
	
	@Transactional(readOnly = false)
	public void delete(SubsidyStandard subsidyStandard) {
		super.delete(subsidyStandard);
	}


	/**
	 * @Author zhangxingliang
	 * @Description  精准获取疫苗补助
	 * @Date 15:47 2019/6/12
	 **/
	public Map<String,Object> findStandardByVaccineNameAndEntName(String vaccineName,String vaccineEntName) {
		if(StringUtils.isBlank(vaccineName)){
			vaccineName = "";
		}
		if(StringUtils.isBlank(vaccineEntName)){
			vaccineEntName = "";
		}
		return this.subsidyStandardDao.findStandardByVaccineNameAndEntName(vaccineName,vaccineEntName);
	}

	/**
	 * @author Zhang Xingliang
	 * @description //通过厂家名获取所有补助标准
	 * @date 15:15 2019/7/11 0011
	 * @param vaccine
	 * @return java.util.List<java.util.Map<java.lang.String,java.lang.Object>>
	 **/
	public List<Map<String, Object>> findStandardByEntName(Vaccine vaccine) {
		return this.subsidyStandardDao.findStandardByEntName(vaccine);
	}

	/**
	 * @Author zhangxingliang
	 * @Description  精准获取疫苗补助
	 * @Date 15:47 2019/6/12
	 **/
	public List<Map<String, Object>> findStandardByVaccine(Vaccine vaccine) {
		return this.subsidyStandardDao.findStandardByVaccine(vaccine);
	}
	/**
	 *
	 * @Title: changeEnt
	 * @author: sy
	 * @date: 2019年06月15日 12:35
	 * @Description: 查询厂家是否已经存过该疫苗
	 * @param subsidyStandard
	 * @return: java.lang.String
	 */
	public String changeEnt(SubsidyStandard subsidyStandard) {
		if(null!=subsidyStandard && subsidyStandard.getEntList()!=null){
			int count = dao.changeEnt(subsidyStandard);
			if(count >0){
				return "error";
			}
		}
		return "success";
	}

	/**
	 *
	 * @Title: findEntListBySubsidyStandardId
	 * @author: sy
	 * @date: 2019年06月15日 13:29
	 * @Description: 根据补助标准id获取厂家list
	 * @param id
	 * @return: java.util.List<com.thinkgem.jeesite.modules.bus.entity.VaccineEnt>
	 */
	public List<VaccineEnt> findEntListBySubsidyStandardId(String id) {
		SubsidyStandard subsidyStandard = this.get(id);
		List<VaccineEnt> list = new ArrayList<>();
		if(StringUtils.isNotBlank(subsidyStandard.getVaccineEntId())){
			List<String> entIdList = Arrays.asList(subsidyStandard.getVaccineEntId().split(","));
			List<String> entNameList = Arrays.asList(subsidyStandard.getVaccineEntName().split(","));
			for(int i=0;i<entIdList.size();i++){
				VaccineEnt vaccineEnt = new VaccineEnt();
				vaccineEnt.setId(entIdList.get(i));
				vaccineEnt.setName(entNameList.get(i));
				list.add(vaccineEnt);
			}
		}
		return list;
	}

	/**
	 *
	 * @Title: formValidate
	 * @author: sy
	 * @date: 2019年06月15日 14:53
	 * @Description: 验证适用企业为全部企业时，疫苗是否存在
	 * @param subsidyStandard
	 * @return: java.lang.String
	 */
	public String formValidate(SubsidyStandard subsidyStandard) {
		int count = dao.formValidate(subsidyStandard);
		if(count>0){
			return "error";
		}
		return "success";
	}
	/*
	 * @MethodName: findSubsidyStandardList
	 * @Description: 疫苗补助列表
	 * @Param: subsidyStandard,page
	 * @Return: String
	 * @Author: my
	 * @Date: 2019/7/23
	 **/
	public Page<SubsidyStandard> findSubsidyStandardList(Page<SubsidyStandard> page, SubsidyStandard subsidyStandard) {
		subsidyStandard.setPage(page);
		page.setList(subsidyStandardDao.findSubsidyStandardList(subsidyStandard));
		return page;
	}

}